<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/3/29
 * Time: 17:22
 * 换货原因
 */
namespace app\admin\controller;
use think\Db;
class Exchange extends Base {

    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('exchange')->count();
            $lists = Db::name('exchange')
                ->page($Nowpage,$limits)
                ->order('exchange_id desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch();
    }

    /*
     * 删除
    */
    public function del(){
        $res = Db::name('exchange')->where('exchange_id',input('id'))->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(request()->isPost ()){
            extract(input());
            $id = input('param.id');
            $param = [
                'exchange_name'=>input('param.fa_title'),
            ];
            $res = Db::name('exchange')->where('exchange_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $info = Db::name('exchange')
            ->where(['exchange_id'=>input('id')])
            ->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 添加
     */
    public function add(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'exchange_name'=>input('param.fa_title'),
                'exchange_add_time'=>$time,
            ];
            $res = Db::name('exchange')->insert ($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return  $this->fetch();
    }
}